package com.jajahome.feature.user.fragment;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jajahome.R;
import com.jajahome.base.BaseFragment;
import com.jajahome.constant.Constant;
import com.jajahome.feature.user.activity.ChipsAct;
import com.jajahome.feature.user.activity.ShoppingCartAct;
import com.jajahome.model.CrowDetailModel;
import com.jajahome.model.CrowPriceModel;
import com.jajahome.model.LoginModle;
import com.jajahome.util.Base64Helper;
import com.jajahome.util.LoginUtil;
import com.jajahome.util.StringUtil;
import com.jajahome.util.T;
import com.jajahome.util.TimeUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by tuxuesong on 2018/5/28.
 */

public class CrowFrag extends BaseFragment implements View.OnClickListener {

    private static String STATUS = "STATUS";
    private static String BEAN = "BEAN";
    private static String POSTAGE = "POSTAGE";
    private static String STATE = "STATE";
    private int mStatus;
    @BindView(R.id.left_img)
    ImageView leftIMG;
    @BindView(R.id.crow_title)
    TextView crowTitle;
    @BindView(R.id.discount)
    TextView disCount;
    @BindView(R.id.origin)
    TextView origin;
    @BindView(R.id.limit_price)
    TextView limitPrice;
    @BindView(R.id.surplus)
    TextView surPlus;
    @BindView(R.id.crow_postage)
    TextView crowPos;
    @BindView(R.id.crow_send_time)
    TextView crowSendTime;
    @BindView(R.id.fid)
    TextView fid;
    @BindView(R.id.fid_imv)
    ImageView fidImg;
    @BindView(R.id.mid)
    TextView mid;
    @BindView(R.id.mid_imv)
    ImageView midImg;
    @BindView(R.id.fid_ll)
    LinearLayout fidLL;
    @BindView(R.id.mid_ll)
    LinearLayout midLL;
    @BindView(R.id.minus)
    TextView plusTv;
    @BindView(R.id.crow_num)
    TextView crowCount;
    @BindView(R.id.add)
    TextView addTv;
    @BindView(R.id.submit)
    TextView submitOk;
    private int count = 1;
    private int postage;
    private GradientDrawable p;
    private Double productNum;
    private String type;
    private String memo;

    private CrowDetailModel.DataEntity.CrowdFundingEntity bean;

    @Override
    protected int getViewId() {
        return R.layout.act_crow_frag;
    }

    @Override
    protected void init() {
        mStatus = getArguments().getInt(STATUS, 0);
        postage = getArguments().getInt(POSTAGE);
        type = getArguments().getString(STATE);
        memo = getArguments().getString(ChipsAct.MEMO);
        bean = (CrowDetailModel.DataEntity.CrowdFundingEntity) getArguments().getSerializable(BEAN);
        setListener();
        setData();
    }

    public static CrowFrag newInstance(int status, CrowDetailModel.DataEntity.CrowdFundingEntity bean, int postage, String type) {
        CrowFrag couFrag = new CrowFrag();
        Bundle b = new Bundle();
        b.putInt(STATUS, status);
        b.putInt(POSTAGE, postage);
        b.putString(STATE, type);
        b.putSerializable(BEAN, bean);
        couFrag.setArguments(b);
        return couFrag;
    }

    private void setData() {
        p = (GradientDrawable) submitOk.getBackground();
        String url = bean.getImage().getThumb();
        if (!StringUtil.isEmpty(url)) {
            Picasso.with(mContext).load(url).placeholder(R.mipmap.newlogo02).into(leftIMG);
        }
        crowTitle.setText(bean.getName());
        disCount.setText(Constant.YUAN + StringUtil.spilStr(bean.getPrice()));
        origin.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中间横线
        origin.setText(Constant.YUAN + StringUtil.spilStr(bean.getOriginal() + ""));
        if (postage == 1) {
            crowPos.setText("运费:" + "包邮");
        } else if (postage == 0) {
            crowPos.setText("运费:" + "不包邮");
        }
        productNum = (Double.parseDouble(bean.getPrice_limit()) - Double.parseDouble(bean.getBuy_count()));
        if (type.equals("0") || type.equals("2") || type.equals("3") || type.equals("4") || type.equals("5")) {
            p.setColor(mContext.getResources().getColor(R.color.b2_b2_b2));
        } else {
            p.setColor(mContext.getResources().getColor(R.color.orange_user));
            //productNum 小于等于0 就不显示  bean.getExceed_limit()是否可以超
            if (bean.getExceed_limit().equals("1") && productNum <= 0) {
                limitPrice.setText("");
                surPlus.setText("");
                submitOk.setEnabled(true);
                p.setColor(mContext.getResources().getColor(R.color.orange_user));
            } else {
                limitPrice.setText("限额" + bean.getPrice_limit() + "份");
                surPlus.setText("剩余" + StringUtil.spilStr(productNum + "") + "份");
                if (productNum <= 0) {
                    submitOk.setEnabled(false);
                    p.setColor(mContext.getResources().getColor(R.color.b2_b2_b2));
                }
            }
        }
        if (bean.getSend_time().equals("0")) {
            crowSendTime.setText("");
        } else {
            crowSendTime.setText("预计发货时间:" + TimeUtil.getDate(Long.parseLong(bean.getSend_time())));
        }
        String thumb = bean.getFid_img().getThumb();
        if (!StringUtil.isEmpty(thumb)) {
            fidLL.setVisibility(View.VISIBLE);
            fid.setText("面料:" + bean.getFid_name());
            Picasso.with(mContext).load(url).placeholder(R.mipmap.newlogo02).into(fidImg);
        } else {
            fidLL.setVisibility(View.GONE);
        }

        String mThumb = bean.getMid_img().getThumb();
        if (!StringUtil.isEmpty(mThumb)) {
            midLL.setVisibility(View.VISIBLE);
            mid.setText("材质:" + bean.getMid_name());
            Picasso.with(mContext).load(url).placeholder(R.mipmap.newlogo02).into(midImg);
        } else {
            midLL.setVisibility(View.GONE);
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.minus:
                count--;
                break;
            case R.id.add:
                if (!bean.getExceed_limit().equals("1")) {
                    if (!StringUtil.isEmpty(productNum + "")) {
                        if (count >= productNum) {
                            T.showShort(mContext, "此件物品当前剩余" + productNum);
                            return;
                        }
                    }
                }
                count++;
                break;
            case R.id.submit:
                jumpToH5();
                break;
        }

        if (count >= 1) {
            crowCount.setText(count + "");
        } else {
            count = 1;
            crowCount.setText(count + "");
        }
    }

    private void setListener() {
        plusTv.setOnClickListener(this);
        addTv.setOnClickListener(this);
        submitOk.setOnClickListener(this);
    }


    private String submit() {

        LoginModle loginModle = LoginUtil.getInfo(mContext);
        if (loginModle == null) {
            return null;
        }
        CrowPriceModel model = new CrowPriceModel();
        model.setId(bean.getId());
        //用户信息
        model.setUser_id(loginModle.getData().getUser().getId()); //
        model.setUser_token(loginModle.getData().getSession());
        model.setItems(getItems());
        String json = new Gson().toJson(model);
        Log.e("json", json);
        return Base64Helper.encodeStringToBase64(json);
    }

    private List<CrowPriceModel.ItemsBean> getItems() {
        List<CrowPriceModel.ItemsBean> list = new ArrayList<>();
        CrowPriceModel.ItemsBean model = new CrowPriceModel.ItemsBean();
        model.setItem_id(bean.getItem_id());
        model.setCount(count + "");
        list.add(model);
        return list;
    }

    private void jumpToH5() {
        if (type.equals("1")) {
            Intent intent = new Intent(getActivity(), ShoppingCartAct.class);
            intent.putExtra(ShoppingCartAct.TYPE, 2);
            intent.putExtra(ShoppingCartAct.H5_URL, Constant.CROW_PRICE + submit());
            mContext.startActivity(intent);
        }

    }

}
